package com.example.hemin.fnb.ui.adapter;

import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.hemin.fnb.R;
import com.example.hemin.fnb.ui.bean.ShoppingCarBean;
import com.example.hemin.fnb.ui.util.AmountView;
import com.example.hemin.fnb.ui.util.AppUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShoppingCarAdapter extends BaseQuickAdapter<ShoppingCarBean, BaseViewHolder> {
    private TextView titl1,titl2;
    private AmountView mAmountView;
    private CheckBox checkBox;
    private boolean stauts = false;
    private  int number = 1;
    private List<Boolean> booleanList = new ArrayList<>();
    private List<ShoppingCarBean> data = new ArrayList<>();

    public ShoppingCarAdapter(int layoutResId, @Nullable List<ShoppingCarBean> data) {
        super(layoutResId, data);
        this.data = data;
        for(int i =0 ;i<data.size();i++){
            booleanList.add(false);
        }
    }
    public void addData(List<ShoppingCarBean> datas){
             data.addAll(datas);
             for(int i = 0;i<datas.size();i++){
                 booleanList.add(false);
             }
             notifyDataSetChanged();
    }
    //更改集合内部存储的状态
    public void initCheck(boolean flag) {

        for (int i = 0; i < data.size(); i++) {
            //更改指定位置的数据
            booleanList.set(i,flag);
        }
    }
    public  void deleteAllData(){
        data.clear();
        booleanList.clear();
        notifyDataSetChanged();
    }
    public  void deletingData(){
        int y = 0;
        for(int i =0;i<data.size();i++){
            if(booleanList.get(i) != null && booleanList.get(i)){
                data.remove(i);
                booleanList.remove(i);
                y++;
                i--;
            }
        }
        notifyDataSetChanged();
        if(y == 0){
            Toast.makeText(AppUtils.getContext(), "没有选中的要删除的数据", Toast.LENGTH_SHORT).show();

        }
    }
    public void selectAll(){
        initCheck(true);
        notifyDataSetChanged();
    }
    public void unSelectAll(){
        initCheck(false);
        notifyDataSetChanged();
    }



    @Override
    protected void convert(final BaseViewHolder helper, final ShoppingCarBean item) {
       helper.setText(R.id.title1,item.getTitl2()).setText(R.id.title2,"￥"+String.valueOf(item.getPice()));
       mAmountView = helper.getView(R.id.add);
        checkBox = helper.getView(R.id.image);
       titl1 = helper.getView(R.id.title1);
       titl2 = helper.getView(R.id.title2);
       if(item.getNumber()<1){
         checkBox.setEnabled(false);
           mAmountView.setVisibility(View.GONE);
           titl1.setTextColor(mContext.getResources().getColor(R.color.c999999));
           titl2.setTextColor(mContext.getResources().getColor(R.color.c999999));
           titl2.setText("已经是人家的宝贝了");
       }else if(item.getNumber()>0){
           checkBox.setEnabled(true);
           mAmountView.setVisibility(View.VISIBLE);
           titl1.setTextColor(mContext.getResources().getColor(R.color.c333333));
           titl2.setTextColor(mContext.getResources().getColor(R.color.cD61616));
       }
       mAmountView.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
           @Override
           public void onAmountChange(View view, int amount) {
                    number = amount;
                    if(stauts == true) {
                        Map<String, Integer> map = new HashMap<>();
                        map.put("number", number);
                        map.put("pice", item.getPice());
                        EventBus.getDefault().post(21, map);
                    }
           }
       });
//       helper.getView(R.id.image).setOnClickListener(new View.OnClickListener() {
//           @Override
//           public void onClick(View v) {
//                if(stauts == false){
//                    helper.getView(R.id.image).setBackgroundResource(R.mipmap.checkbox_red);
//                    Map<String, Integer> map = new HashMap<>();
//                      map.put("number",number);
//                      map.put("pice",item.getPice());
//                   EventBus.getDefault().post(21,map);
//                   stauts = true;
//                   booleanList.set(helper.getAdapterPosition(),stauts);
//                }else if(stauts == true){
//                    helper.getView(R.id.image).setBackgroundResource(R.mipmap.checkbox_balck);
//                    stauts = false;
//                    booleanList.set(helper.getAdapterPosition(),false);
//                }
//           }
//       });

           checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
               @Override
               public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                   if(item.getNumber()<1){
//                   booleanList.set(helper.getAdapterPosition(),false);
                       checkBox.setChecked(false);
                   }else if(item.getNumber()>0) {
                       booleanList.set(helper.getAdapterPosition(), isChecked);
                   }
               }
           });
           checkBox.setChecked(booleanList.get(helper.getAdapterPosition()));
    }
}
